#!/bin/bash

# Run from source root
# e.g. ./pants bundle scrooge/scrooge-linter:app \
#      ./scrooge/scrooge-linter/src/scripts/linter-test dist/scrooge-linter-package-bundle/bin.jar

set -e

args=("$@")
cp=${args[0]}

linterRun="java -cp $cp com.twitter.scrooge.linter.Main -n scrooge/scrooge-linter/src/test/thrift"

echo "Running linter (should pass)"
linter_pass="scrooge/scrooge-linter/src/test/thrift/pass.thrift"

$linterRun $linter_pass

echo "Running linter (should warn)"
linter_warn=`find scrooge/scrooge-linter/src/test/thrift/warn -name *.thrift`
for input in $linter_warn; do
   cmd_out=`$linterRun -w $input 2>&1`
   if [[ $cmd_out =~ "LINT-WARN" ]] ; then
     echo "Warnings found as expected while linting $input"
   else
     echo "ERROR: Lint warnings expected when linting $input, no warn found."
     echo command line: $linterRun $input &&  exit 1
   fi
done

set +e
echo "Running linter (should fail)"
linter_fail=`find scrooge/scrooge-linter/src/test/thrift/fail -name *.thrift`

for input in $linter_fail; do
    $linterRun $input  && echo RETURNED $? && echo ERROR: Lint errors expected when linting $input, no error found. Exiting.&&\
                         echo command line: $linterRun $input &&  exit 1
done

set -e
